package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.redis.Service.IUserInfoRedisService;
import cn.wolfcode.luowowo.repository.UserInfoRepository;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.AssertUtil;
import cn.wolfcode.luowowo.util.Consts;
import cn.wolfcode.luowowo.util.LogicException;
import cn.wolfcode.luowowo.util.MobelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService{
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Override
    public UserInfo selectById(String id) {
        return userInfoRepository.findById(id).get();
    }

    /**
     * 判断手机号码是否已经注册过
     * @param phone
     * @return
     */
    @Override
    public boolean selectByPhone(String phone) {
        if(userInfoRepository.findByPhone(phone) != null){
            return true;
        }else{
            return false;
        }

    }


    @Override
    public String sendVerifyCode(String phone, String ssmUrl, String ssmAppkey, String ssmSign) {
        //获取验证码
        String code = UUID.randomUUID().toString().replaceAll("-","")
                .substring(0, 4);
        StringBuilder stringBuilder = new StringBuilder("短信发送成功,您的验证码是:")
                .append(code)
                .append(",验证码将于").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟之后GG");
        System.out.println(stringBuilder.toString());
        //java中发送http请求--->发送短信
        RestTemplate template = new RestTemplate();
        String s = template.getForObject(ssmUrl,String.class,ssmSign,phone,stringBuilder.toString(),ssmAppkey);
        System.out.println(s);
        if(!s.contains("SUCCESS")){
            throw new LogicException("短信发送失败");
        }
        return code;
    }

    /**
     * 校验
     * @param phone 手机号码
     * @param verifyCode 验证码
     * @param nickname 用户名
     * @param password 第一次输入的密码
     * @param rpassword 第二次输入的密码
     */
    @Override
    public void regist(String phone, String verifyCode, String nickname, String password, String rpassword) {
        //参数是否为空
        AssertUtil.hasLength(phone,"手机号码不能为空");
        AssertUtil.hasLength(verifyCode,"验证码不能为空");
        AssertUtil.hasLength(nickname,"用户名不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"密码不能为空");

        //手机号码是否符合格式
        if(!MobelUtil.isMobile(phone)){
            throw new LogicException("手机号码不符合格式");
        }

        //手机号码是否已经注册
        if (selectByPhone(phone)){
            throw new LogicException("手机号码已经注册过");
        }

        //前后两次输入的密码是否为空或者一致
        if(password == null || rpassword == null){
            throw new RuntimeException("密码不能为空");
        }else if(!(password.equals(rpassword))){
            throw new LogicException("前后两次输入的密码不一致");
        }

        //验证码是否一致
        String key = "verify_code:" + phone;
        String code1 = userInfoRedisService.selectByKey(key);
        if(verifyCode == null || verifyCode.equals(code1)){
            throw new LogicException("验证码失效或不一致");
        }

        //注册
        UserInfo userInfo = new UserInfo();
        userInfo.setCity("");
        userInfo.setEmail("");
        userInfo.setLevel(1);
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setInfo("");
        userInfo.setNickname(nickname);
        userInfo.setPassword(password);
        userInfo.setPhone(phone);
        userInfo.setState(UserInfo.STATE_NORMAL);
        userInfoRepository.save(userInfo);
    }

    /**
     * 校验参数并通过账号密码查询数据库中是否存在该用户
     * @param username
     * @param password
     * @return
     */
    @Override
    public UserInfo login(String username, String password) {
        //判断参数是否为空
        AssertUtil.hasLength(username,"用户名不能为空");
        AssertUtil.hasLength(password,"密码不能为空");

        UserInfo userInfo = userInfoRepository.findByPhone(username);
        if (userInfo!=null && userInfo.getPassword().equals(password)){
           //如果用户存在且密码正确,说明用户存在数据库,要返回该用户回去
            userInfo.setPassword(null);
            return userInfo;
        }
        return null;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoRepository.findAll();
    }

    @Override
    public UserInfo get(String id) {
        return userInfoRepository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> findByCity(String keyword) {

        return userInfoRepository.findByCity(keyword);
    }

    @Override
    public List<UserInfo> fingByIdIn(List ids) {
        return userInfoRepository.findByIdIn(ids);
    }

    @Override
    public void update(UserInfo userInfo1) {
        userInfoRepository.save(userInfo1);
    }
}
